package com.fyzk.activity.popupwindow;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.fyzk.activity.lottery.recommend_order.OtherCenterActivity;
import com.fyzk.activity.lottery.recommend_order.RankBean;
import com.fyzk.activity.lottery.recommend_order.WinningRankBean;
import com.fyzk.bean.DocumentaryMenuResponse;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.StringUtils;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;
import razerdp.basepopup.BasePopupWindow;

/**
 * 中奖榜单弹窗
 */
public class WinningRankPopup extends BaseRequestPopupWindow implements View.OnClickListener {

    private ImageView iv_close;
    LinearLayout mLlNo1;
    ImageView mIvAvatarNo1;
    TextView mTvNameNo1;
    TextView mTvRedNo1;
    LinearLayout mLlNo2;
    ImageView mIvAvatarNo2;
    TextView mTvNameNo2;
    TextView mTvRedNo2;
    LinearLayout mLlNo3;
    ImageView mIvAvatarNo3;
    TextView mTvNameNo3;
    TextView mTvRedNo3;

    private List<WinningRankBean> rankList;

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_winning_rank);
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return getDefaultScaleAnimation(false);
    }

    public WinningRankPopup(Context context) {
        super(context);
        setPopupGravity(Gravity.CENTER);
        setOutSideDismiss(false);
        setBackPressEnable(false);

        initView();
        initListener();
    }

    public void setRankData(List<WinningRankBean> rankList) {
        this.rankList = rankList;
        showRankData();
    }

    private void initView() {
        iv_close = findViewById(R.id.iv_close);
        mLlNo1 = findViewById(R.id.ll_no1);
        mIvAvatarNo1 = findViewById(R.id.iv_avatar_no1);
        mTvNameNo1 = findViewById(R.id.tv_name_no1);
        mTvRedNo1 = findViewById(R.id.tv_red_no1);
        mLlNo2 = findViewById(R.id.ll_no2);
        mIvAvatarNo2 = findViewById(R.id.iv_avatar_no2);
        mTvNameNo2 = findViewById(R.id.tv_name_no2);
        mTvRedNo2 = findViewById(R.id.tv_red_no2);
        mLlNo3 = findViewById(R.id.ll_no3);
        mIvAvatarNo3 = findViewById(R.id.iv_avatar_no3);
        mTvNameNo3 = findViewById(R.id.tv_name_no3);
        mTvRedNo3 = findViewById(R.id.tv_red_no3);
    }

    private void initListener() {
        iv_close.setOnClickListener(this);
        mLlNo1.setOnClickListener(this);
        mLlNo2.setOnClickListener(this);
        mLlNo3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close://关闭
                if (mOnClickListener != null)
                    mOnClickListener.onCloseClick(WinningRankPopup.this,v);
                dismiss();
                break;
            case R.id.ll_no1://第一名
                startOtherCenterActivity(0);
                break;
            case R.id.ll_no2://第二名
                startOtherCenterActivity(1);
                break;
            case R.id.ll_no3://第三名
                startOtherCenterActivity(2);
                break;
        }
    }

    /**
     * 打开他的中心
     *
     * @param index
     */
    private void startOtherCenterActivity(int index) {
        if (rankList.size() <= index)
            return;
        WinningRankBean rankBean = rankList.get(index);
        Intent intent = new Intent(getContext(), OtherCenterActivity.class);
        if (rankBean != null) {
            intent.putExtra(IntentParamUtils.USERNO, rankBean.getUserno());
            getContext().startActivity(intent);
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        switch (requestCode) {
            case CommandUtil.ORDERWINRROUNDS:
                rankList = JSON.parseArray(resultBean.getData(), WinningRankBean.class);
                showRankData();
                break;
        }
    }

    /**
     * 展示排行榜信息
     */
    private void showRankData() {
        if (rankList == null || rankList.isEmpty()) {
            Glide.with(getContext())
                    .load("")
                    .circleCrop()
                    .error(R.drawable.rr_header_icon)
                    .placeholder(R.drawable.rr_header_icon)
                    .into(mIvAvatarNo1);
            mTvNameNo1.setText("");
            mTvRedNo1.setText("");
            Glide.with(getContext())
                    .load("")
                    .circleCrop()
                    .error(R.drawable.rr_header_icon)
                    .placeholder(R.drawable.rr_header_icon)
                    .into(mIvAvatarNo2);
            mTvNameNo2.setText("");
            mTvRedNo2.setText("");
            Glide.with(getContext())
                    .load("")
                    .circleCrop()
                    .error(R.drawable.rr_header_icon)
                    .placeholder(R.drawable.rr_header_icon)
                    .into(mIvAvatarNo3);
            mTvNameNo3.setText("");
            mTvRedNo3.setText("");
            return;
        }
        for (int i = 0; i < rankList.size(); i++) {
            WinningRankBean rankBean = rankList.get(i);
            String redText = String.format("收米\n%s",StringUtils.getDoubleString(rankBean.getPretaxPrize()*100));
            switch (i) {
                case 0://第一名
                    Glide.with(getContext())
                            .load(rankBean.getPhotoUrl())
                            .circleCrop()
                            .error(R.drawable.rr_header_icon)
                            .placeholder(R.drawable.rr_header_icon)
                            .into(mIvAvatarNo1);
                    mTvNameNo1.setText(StringUtils.formatBindMobile(rankBean.getUserName()));
                    mTvRedNo1.setText(redText);
                    break;
                case 1://第二名
                    Glide.with(getContext())
                            .load(rankBean.getPhotoUrl())
                            .circleCrop()
                            .error(R.drawable.rr_header_icon)
                            .placeholder(R.drawable.rr_header_icon)
                            .into(mIvAvatarNo2);
                    mTvNameNo2.setText(StringUtils.formatBindMobile(rankBean.getUserName()));
                    mTvRedNo2.setText(redText);
                    break;
                case 2://第三名
                    Glide.with(getContext())
                            .load(rankBean.getPhotoUrl())
                            .circleCrop()
                            .error(R.drawable.rr_header_icon)
                            .placeholder(R.drawable.rr_header_icon)
                            .into(mIvAvatarNo3);
                    mTvNameNo3.setText(StringUtils.formatBindMobile(rankBean.getUserName()));
                    mTvRedNo3.setText(redText);
                    return;
            }
        }
    }

    @Override
    public boolean onBeforeDismiss() {
        rankList.clear();
        showRankData();
        return super.onBeforeDismiss();
    }

    private onClickListener mOnClickListener;

    public interface onClickListener {
        /**
         * 关闭按钮点击事件
         * @param popupWindow
         * @param view
         */
        void onCloseClick(BasePopupWindow popupWindow,View view);
    }

    public void setOnClickListener(onClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }
}
